package cn.itsource.org.service.impl;

import cn.itsource.basic.basic.BusinessException;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.BaiduAuditUtils;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.dto.ShopDto;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopAuditLogMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.IShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;
/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper logMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {
        //1.检验 - 空值校验：前端的js校验容易被禁用，不安全，后端也要做校验
        if (StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("提交的资料不能为空哦");
        }

        //检验店铺名称
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名称含有不良信息,请重新设置");
        }

        //校验logo
        if(!StringUtils.isEmpty(shop.getLogo())){
            if(!BaiduAuditUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){
                throw new BusinessException("logo不符合社区规定，请更换");
            }
        }

        //2.校验 - 当前店铺是否入驻过
        Shop dbShop = shopMapper.findByNameAndAddress(shop);
        if (dbShop != null){
            throw new BusinessException("店铺已经入驻过了哦，忘记密码请联系管理员");
        }

        //3.数据库操作
        //保存t_employee员工信息 - 生成自增id
        Employee admin = shop.getAdmin();
        employeeMapper.add(admin);
        //将自增id设置到shop对象，并保存店铺信息 - 生成店铺的自增id
        shop.setAdminId(admin.getId());
        shopMapper.add(shop);
        //将店铺的shopId设置到employee对象，修改employee信息
        admin.setShopId(shop.getId());
        employeeMapper.update(admin);
    }

    @Override
    public void pass(ShopAuditLog log) throws MessagingException {
        //改状态
        Shop shop = shopMapper.findById(log.getShopId());
        shop.setState(2);
        shopMapper.update(shop);

        //保存审核日志
        log.setState(2);
        //log.getAuditId("");
        logMapper.add(log);

        //发邮件
        //创建复杂的邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //true--能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("2464338180@qq.com");
        helper.setSubject("店铺审核邮件");
        //true--能够在内容中编写html标签
        helper.setText("<h3>你的店铺已经审核通过，请<a href='http://127.0.0.1:8080/shop/active/"+log.getShopId()
                +"'>点击这里</a>激活邮件</h3>", true);
        Employee admin = employeeMapper.findById(shop.getAdminId());
        helper.setTo(admin.getEmail());
        //发送
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        //改状态
        Shop shop = shopMapper.findById(log.getShopId());
        shop.setState(4);
        shopMapper.update(shop);

        //保存审核日志
        log.setState(4);
        //log.getAuditId("");
        logMapper.add(log);

        //发邮件
        //创建复杂的邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //true--能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("2464338180@qq.com");
        helper.setSubject("店铺激活失败邮件");
        //true--能够在内容中编写html标签
        helper.setText("<h3>你的店铺审核未通过，请<a href='http://localhost:8081/#/regiseterEdit/\"+log.getShopId()\n" +
                "                +\"'>点击这里</a>修改入驻信息重新提交</h3>",true);
        Employee admin = employeeMapper.findById(shop.getAdminId());
        helper.setTo(admin.getEmail());
        //发送
        javaMailSender.send(mimeMessage);
    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }

}
